<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 9:15
 */

namespace App\GraphQL\Mutations;

use Closure;
use App\User;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\Mutation;

//批量更新用户信息
class UpdateUsersMutation extends Mutation
{

    protected $attributes = [
        'name' => 'updateUsersMut'
    ];

    public function type(): Type
    {
        return Type::listOf(\GraphQL::type('users'));
    }

    public function args(): array
    {
        return [
            'products' => [
                'name' => 'products',
                'type' => \GraphQL::Type('[UpdateUsersInput!]!')
            ],

        ];
    }



    public function resolve($root, $args)
    {

/*
          //注意最外面不需要大括号, 小写的mutation
          mutation updateUsersAss($pros: [UpdateUsersType!]!){
            updateUsers(products: $pros){
              id
              name
            }
          }

          variables: {
           "pros" : [
                {
                  "id": 2,
                  "name": "6666666666666"
                }
            ]
          }
*/



        $products = $args['products'];
        $ids = [];

        foreach ($products as $v) {
            $ids[] = $v['id'];
            $user = User::find($v['id']);
            if (!$user) {
                return null;
            }
            $user->name = $v['name'];
            $user->save();

        }

        $users = User::whereIn('id', $ids)->get();

        return $users; //返回是一个list


    }








}
